﻿using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using SjclHelpers;
using SjclHelpers.Codec;
using SjclHelpers.Misc;

namespace SjclHelpersTests.Misc
{
    [TestClass]
    public class EncryptionTests
    {
        [TestMethod]
        [ExpectedException(typeof(ArgumentNullException))]
        public void Decrypt_NoKey_ArgumentNullException()
        {
            // Fixture setup...

            // Exercise and verify SUT...
            new byte[] { }.Decrypt(null, null);
        }

        [TestMethod]
        [ExpectedException(typeof(ArgumentNullException))]
        public void Decrypt_NoIV_ArgumentNullException()
        {
            // Fixture setup...

            // Exercise and verify SUT...
            new byte[] { }.Decrypt(new byte[] { }, null);
        }

        [TestMethod]
        public void Decrypt_Works()
        {
            // Fixture setup...
            const string expectedPlainText = "secret";
            const string password = "password";
            const string salt = "5f9bcef98873d06a";
            var saltBytes = salt.ToBytes();
            var derivedKey = password.DeriveKey(saltBytes);
            // Value of derived key in hex: fb8e8781defbad9fa12cb1395d270457

            const string initalizationVector = "8291ff107e798a29";
            var initalizationVectorBytes = initalizationVector.ToBytes();
            const string cipherText = "6aa81845a77992c0e5c1431d4be2";
            var cypherBytes = cipherText.ToBytes();

            // Exercise SUT...
            var actualPlainText =
                cypherBytes.Decrypt(derivedKey, initalizationVectorBytes);

            // Verify SUT...
            Assert.AreEqual(expectedPlainText, actualPlainText);
        }
    }
}
